package team.you.change.util;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import team.you.change.dao.ClassMapper;
import team.you.change.dao.CollegeMapper;
import team.you.change.dao.UserMapper;
import team.you.change.pojo.Class;
import team.you.change.pojo.College;

import java.util.*;

/**
 * 随机生成班级数据
 */
@RunWith(SpringRunner.class)
@ContextConfiguration({"classpath*:mapper/*.xml","classpath:spring-mybatis.xml"})
public class ClassRandomCreate {
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private CollegeMapper collegeMapper;
    @Autowired
    private UserMapper userMapper;
    private static final Logger LOGGER = LogManager.getRootLogger();

    private static List<Integer> COLLEGE;
    private static List<Integer> MAJOR;
    private static List<String> TUTOR;
    private static String[] PHONE = {"159", "158", "136", "135", "147"};

    @Test
    public void create(){
        getCollege();
        getTutor();
        for(int i = 0; i < 10; i++){
            Class cla = new Class();
            Random random = new Random();
            String uuid = UUID.randomUUID().toString();
            cla.setId(uuid);
            cla.setClassCode(getClassCode());
            cla.setClassNumber(random.nextInt(30) + 20);
            int id = random.nextInt(COLLEGE.size());
            getMajor(COLLEGE.get(id));
            cla.setCollege(COLLEGE.get(id));
            cla.setMajor(MAJOR.get(random.nextInt(MAJOR.size())));
            cla.setClassLink(getName());
            cla.setClassLinkPhone(getPhone());
            cla.setClassTutor(TUTOR.get(random.nextInt(TUTOR.size())));
            int insert = classMapper.insert(cla);
            if (insert > 0){
                LOGGER.info("成功");
            }else LOGGER.info("失败");
        }
    }

    /**
     * 随机生成一个电话号码
     * @return 电话号码
     */
    public static String getPhone(){
        Random random = new Random();
        StringBuilder rs = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            rs.append(random.nextInt(10));
        }
        return PHONE[random.nextInt(PHONE.length)] + rs.toString();
    }

    /**
     * 随机生成一个名称
     * @return 名称
     */
    private String getName(){
        Random random = new Random();
        int i = random.nextInt(899) + 100;
        return "test" + i;
    }

    /**
     * 随机生成一个班级代码
     * @return 班级代码
     */
    private String getClassCode(){
        StringBuilder sb = new StringBuilder("117");
        Random random = new Random();
        int one = random.nextInt(10);
        int two = random.nextInt(10);
        int three = random.nextInt(10);
        sb.append("0" + one);
        sb.append("0" + two);
        sb.append("0" + three);
        return sb.toString();
    }

    /**
     * 获取所有学院id
     */
    private void getCollege(){
        List<College> colleges = collegeMapper.selectAllCollId();
//        colleges.forEach(e -> COLLEGE.add(e.getId()));
        Iterator<College> iterator = colleges.iterator();
        List<Integer> list = new ArrayList<>();
        while (iterator.hasNext()){
            list.add(iterator.next().getId());
        }
        COLLEGE = list;
    }

    /**
     * 根据学院id获取专业id
     * @param id 所属学院id
     */
    private void getMajor(Integer id){
        List<College> colleges = collegeMapper.selectAllMajorId(id);
//        colleges.forEach(e -> MAJOR.add(e.getId()));
        Iterator<College> iterator = colleges.iterator();
        List<Integer> list = new ArrayList<>();
        while (iterator.hasNext()){
            list.add(iterator.next().getId());
        }
        MAJOR = list;
    }

    /**
     * 获取所有用户id
     */
    private void getTutor(){
        TUTOR = userMapper.selectListId();
    }
}
